package com.service.impl;

import com.Exception.NormalException.FileExceptions.FileFormatException;
import com.WebSocket.FriendDelete;
import com.WebSocket.GroupDelete;
import com.WebSocket.SocketData;
import com.WebSocket.WebSocket;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.entity.Group;
import com.entity.GroupUser;
import com.entity.User;
import com.mapper.GroupMapper;
import com.mapper.GroupUserMapper;
import com.service.GroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.GroupUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XTC
 * @since 2024-07-10
 */
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements GroupService {
    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private GroupUserMapper groupUserMapper;

    @Autowired
    private GroupUserService groupUserService;

    @Autowired
    private FileService fileService;

    @Override
    public boolean add(Group group) {
        group.setHeadShotUrl("/head/default.jpg");
        return this.groupMapper.insert(group) > 0;
    }

    @Override
    public List<Group> getMyGroup(Integer userId) {
        QueryWrapper<Group> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("master_id", userId);
        return this.groupMapper.selectList(queryWrapper);
    }

    @Override
    public boolean delete(Integer groupId) {
        QueryWrapper<GroupUser> groupUserQueryWrapper = new QueryWrapper<GroupUser>();
        groupUserQueryWrapper.eq("group_id", groupId);
        this.groupUserMapper.delete(groupUserQueryWrapper);
        QueryWrapper<Group> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",groupId);
        this.sendGroupDelete(groupId);
        this.groupMapper.delete(queryWrapper);
        return true;
    }

    private void sendGroupDelete(Integer groupId) {
        Group group = this.groupMapper.selectById(groupId);
        GroupDelete groupDelete = new GroupDelete();
        groupDelete.setGroupName(group.getName());
        groupDelete.setGroupId(groupId);
        SocketData socketData = new SocketData();
        socketData.setType("group_delete");
        socketData.setGroupDelete(groupDelete);

        List<User> userList = this.groupUserService.getAllUserOfGroup(groupId);
        for(User user : userList) {
            WebSocket.sendOneMessage(user.getId(), socketData);
        }
    }

    @Override
    public String uploadHead(Integer groupId, MultipartFile head) throws FileFormatException, IOException {
        String url = this.fileService.saveFile(head, "head/groupHead/", String.valueOf(groupId), false);
        UpdateWrapper<Group> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", groupId).set("head_shot_url", url);
        this.groupMapper.update(null, updateWrapper);
        return url;
    }
}
